<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['from_pluginname']){
	$_SESSION['from_pluginname']=$_RQ['from_pluginname'];
}
$return=array();
if($_RQ['op']=='edit'){
	//新增编辑公众号
	$return['item']=pdo_get('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
	
	if($_RQ['postdata']){
		$post=get_postdata();
		if(empty($post['type'])){
			exi('请选择公众号类型！','error');
		}
		$data=array(
			'type'=>intval($post['type']),
			'title'=>trim($post['title']),
			'appid'=>trim($post['appid']),
			'appsecret'=>trim($post['appsecret']),
			'oldid'=>trim($post['oldid']),
			'yindao'=>trim($post['yindao']),
		);
		check_wechat($data['appid'],$data['appsecret']);
		if(!empty($return['item'])){
			pdo_update('core_account_wechat',$data,array('uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
			
		}else{
			//检查公众号是否已存在
			
			$check=pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'oldid'=>$data['oldid']),'id');
			if($check){
				exi('您已添加过该公众号了！请勿重复添加！','error');
			}
			$data['is_open']=1;
			$data['isdefault']=1;
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('core_account_wechat',$data);
			
		}
		exi('设置成功！');
	}
	$return['account']=pdo_get('core_accounts',array('uniacid'=>$_SESSION['uniacid']),array('token','aeskey'));
	$return['account']['url']=SITEROOT.'api/wxapi.php?id='.$_SESSION['uniacid'];
	$return['item']['url']=$return['account']['url'];
	$return['item']['token']=$return['account']['token'];
	$return['item']['aeskey']=$return['account']['aeskey'];
	$htmls=array(
		'type'=>'edit',
		'edits'=>array(
			array(
				'title'=>'公众号资料（【温馨提示】:JS域名安全认证文件上传？统一在：站点设置--认证文件上传！）',
				'list'=>array(
					array('type'=>'string','title'=>'公众号名称','name'=>'title', 'help'=>'' ,'must'=>true),
					array('type'=>'radio','title'=>'公众号类型','name'=>'type', 'options'=>'types','help'=>'请设置正确的公众号类型，以免出错！' ,'must'=>true),
					array('type'=>'string','title'=>'AppID','name'=>'appid','help'=>'填写公众号开发者AppID。登陆微信公众平台——开发——基本配置——开发者ID' ,'must'=>true),
					array('type'=>'string','title'=>'AppSecre','name'=>'appsecret','help'=>'填写公众号开发者AppSecre。登陆微信公众平台——开发——基本配置——开发者密钥' ,'must'=>true),
					array('type'=>'string','title'=>'原始ID','name'=>'oldid' , 'help'=>'填写公众号原始ID。登陆微信公众平台——设置——公众号设置——帐号详情——原始ID'),
					//array('type'=>'image','title'=>'公众号logo','name'=>'logo'),
					array('type'=>'switch','title'=>'是否设置为默认公众号','name'=>'isdefault'),
				)
			),
			array(
				'title'=>'公众号绑定方法（授权模式添加公众号请忽略此步骤）：根据提示填写上述参数之后，将以下三项参数填至微信公众平台——开发——基本配置——服务器配置，启用，提交即可。',
				'list'=>array(
					array('type'=>'copy','title'=>'服务器地址','name'=>'url', 'help'=>'' ),
					array('type'=>'copy','title'=>'令牌（Token）','name'=>'token','help'=>'' ),
					array('type'=>'copy','title'=>'消息加解密密钥(EncodingAESKey)','name'=>'aeskey','help'=>'' ),
				)
			)
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
